package targetToOffer.sixthDay;

import java.util.HashMap;

/**
 * @author Godc
 * @description
 */
public class LengthOfLongestSubstring {
    public static void main(String[] args) {
        LengthOfLongestSubstringSolution s = new LengthOfLongestSubstringSolution();
        int l = s.lengthOfLongestSubstring("au");
        System.out.println(l);
    }
}
class LengthOfLongestSubstringSolution {
    /**
     * 滑动窗口
     * @param s
     * @return
     */
    public int lengthOfLongestSubstring(String s) {
        if(s.length()==0) return 0;
        HashMap<Character, Integer> map = new HashMap<>();
        int left = 0,max = 0;
        for (int i = 0; i < s.length(); i++) {
            if(map.containsKey(s.charAt(i))) {
                // 防止left回退
                left = Math.max(left,map.get(s.charAt(i)) + 1);
            }
            map.put(s.charAt(i),i);
            max = Math.max(max,i-left+1);
        }
        return max;
    }

}